package com.wuxf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wuxf.entity.ConsumeInfo;
import com.wuxf.entity.PwdInfo;
import com.wuxf.entity.ResponseBodyEntity;
import com.wuxf.entity.UserInfo;
import com.wuxf.entity.custom.ConsumeCustom;
import com.wuxf.entity.custom.PwdCustom;
import com.wuxf.service.CarAreaService;
import com.wuxf.service.CarCompanyService;
import com.wuxf.service.ConsumeService;
import com.wuxf.service.DicDataService;
import com.wuxf.service.PwdService;
import com.wuxf.service.SqlserverService;
import com.wuxf.service.UserService;

import lombok.extern.slf4j.Slf4j;

@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("app")
@Slf4j
public class AppController {
	@Autowired
	private CarAreaService carAreaService;
	@Autowired
	private CarCompanyService carCompanyService;
	@Autowired
	private ConsumeService consumeService;
	@Autowired
	private DicDataService dicDataService;
	@Autowired
	private SqlserverService sqlserverService;
	@Autowired
	private UserService userService;
	@Autowired
	private PwdService pwdService;
	@Value("${sqlserverUrl}")
	private String sqlserverUrl;
	
	
	@RequestMapping("findCarArea")
	public ResponseBodyEntity findCarArea(String areaId) throws Exception {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		responseBodyEntity.setCode(0);
		HashMap hashMap = new HashMap();
		hashMap.put("areaId", areaId);
		responseBodyEntity.setData(carAreaService.findByMap(hashMap));
		return responseBodyEntity;
	}
	@RequestMapping("findCarCompany")
	public ResponseBodyEntity findCarCompany(String company) throws Exception {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		responseBodyEntity.setCode(0);
		HashMap hashMap = new HashMap();
		hashMap.put("company", company);
		responseBodyEntity.setData(carCompanyService.findByMap(hashMap));
		return responseBodyEntity;
	}
	
	@RequestMapping("findAreaList")
	public ResponseBodyEntity findAreaList() throws Exception {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(dicDataService.findByEnumTypeId("2"));
		return responseBodyEntity;
	}
	
	@RequestMapping("findUnitList")
	public ResponseBodyEntity findUnitList() throws Exception {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(dicDataService.findByEnumTypeId("4"));
		return responseBodyEntity;
	}
	/**
	 * 查询地磅系统最新信息和密码相关信息
	 * @param pwdValue
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findByNew")
	public Map findByNew(String pwdValue) throws Exception {
		Map resultMap = new HashMap();
		Map temp = new HashMap();
		try {
			temp = sqlserverService.findByMap();
		}catch (Exception e) {
			e.printStackTrace();
			log.error("sqlserver数据库链接异常:{}",e.getMessage());
		}
		HashMap paraMap = new HashMap();
		paraMap.put("state", "1");
		paraMap.put("pwdValue", pwdValue);
		List<PwdCustom> pwdCustoms = pwdService.findByMap(paraMap);
		if(pwdCustoms!=null && pwdCustoms.size()>0) {
			PwdCustom pwdCustom = pwdCustoms.get(0);
			UserInfo userInfo = userService.findByUserId(pwdCustom.getCreateUser());
			temp.put("invoiceType", dicDataService.findByEnumTypeId("1",pwdCustom.getTicketType()).get(0).getDicDataName());
			temp.put("areaName", dicDataService.findByEnumTypeId("2",userInfo.getAreaId()+"").get(0).getDicDataName());
			resultMap.put("jsonStr", temp);
			resultMap.put("roughWeight",temp.get("毛重"));
			resultMap.put("netWeight",temp.get("净重"));
			resultMap.put("carNo",temp.get("车号"));
			resultMap.put("ticketType", pwdCustom.getTicketType());
			resultMap.put("areaId", userInfo.getAreaId());
			resultMap.put("pwdId", pwdCustom.getPwdId());
			resultMap.put("state", "SUCCEED");
		}else {
			resultMap.put("state", "failure");
			resultMap.put("msg", "密码无效!");
		}
		return resultMap;
	}
	
	/**
	 * 提交过车信息
	 * @param consumeCustom
	 * @return
	 */
	@RequestMapping("/saveConsumeInfo")
	public Map saveConsumeInfo(ConsumeCustom consumeCustom,String pwdValue) {
		Map resultMap = new HashMap();
		if(StringUtils.isNotBlank(pwdValue)) {
			ConsumeInfo consumeInfo = new ConsumeInfo();
			BeanUtils.copyProperties(consumeCustom, consumeInfo);
			HashMap paraMap = new HashMap();
			paraMap.put("pwdValue", pwdValue);
			paraMap.put("state", "1");
			PwdInfo pwdInfo = pwdService.findByMap(paraMap).get(0);
			consumeService.saveConsume(consumeInfo,pwdInfo);
			resultMap.put("state", "success");
			resultMap.put("msg", "保存过车信息成功");
		}else {
			resultMap.put("state", "failure");
			resultMap.put("msg", "票据密码为空");
		}
		return resultMap;
	}
	
}
